﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebTemplate.DAL.Entities;
using System.Data.SqlClient;
using System.Configuration;

namespace WebTemplate.DAL.Repositories
{
    public class MemberReprositry
    {
        public void Insert(Members Item)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Member_Insert]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@FirstName", Item.FirstName);
            command.Parameters.AddWithValue("@LastName", Item.LastName);
            command.Parameters.AddWithValue("@Email", Item.Email);
            command.Parameters.AddWithValue("@Password", Item.Password);
            command.Parameters.AddWithValue("@UserID", Item.UserID);
            command.Parameters.AddWithValue("@IsDoctor", Item.IsDoctor);
            command.Parameters.AddWithValue("@IsActive", Item.IsActive);
            command.Parameters.AddWithValue("@IsReset" , Item.IsReset);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public void Update(Members Item)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Member_Update]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@ID", Item.Id);
            command.Parameters.AddWithValue("@FirstName", Item.FirstName);
            command.Parameters.AddWithValue("@LastName", Item.LastName);
            command.Parameters.AddWithValue("@Email", Item.Email);
            command.Parameters.AddWithValue("@Password", Item.Password);
            command.Parameters.AddWithValue("@UserID", Item.UserID);
            command.Parameters.AddWithValue("@IsDoctor", Item.IsDoctor);
            command.Parameters.AddWithValue("@IsActive", Item.IsActive);
            command.Parameters.AddWithValue("@IsReset", Item.IsReset);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public void Delete(int id)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Member_Delete]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@Id", id);

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public List<Members> Select(MembersSearchCriteria Item)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Member_Select]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@Id", Item.Id);
            command.Parameters.AddWithValue("@FirstName", Item.FirstName);
            command.Parameters.AddWithValue("@LastName", Item.LastName);
            command.Parameters.AddWithValue("@Email", Item.Email);
            command.Parameters.AddWithValue("@Password", Item.Password);
            command.Parameters.AddWithValue("@UserID", Item.UserID);
            command.Parameters.AddWithValue("@IsDoctor", Item.IsDoctor);
            command.Parameters.AddWithValue("@IsActive", Item.IsActive);
            command.Parameters.AddWithValue("@IsReset", Item.IsReset);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            List<Members> reg = new List<Members>();

            while (reader.Read())
            {
                Members obj = new Members();

                obj.Id = reader.GetInt32(0);
                obj.FirstName = reader.GetString(1);
                obj.LastName = reader.GetString(2);
                obj.Email = reader.GetString(3);
                obj.Password = reader.GetString(4);
                obj.UserID = reader.GetString(5);
                obj.IsDoctor = reader.GetBoolean(6);
                obj.IsActive = reader.GetBoolean(7);
                //obj.IsReset = reader.GetString(8);

                reg.Add(obj);
            }

            connection.Close();

            return reg;
        }
    
    }
        
}
